"""
Case Type   : 小型化
Case Name   : ssl证书路径正确
Create At   : 2024/10/31
Owner       : ningyali
Description :
    1.获取安装包并解压
    2.安装时ssl证书路径正确
    3.清理数据环境
Expect      :
    1.成功
    2.失败
    3.清理数据环境成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.Common import Common
from testcase.utils.Logger import Logger


class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.Common = Common()
        self.root_nodes = (Node('PrimaryRoot'),)
        self.user_nodes = (Node('PrimaryDbUser'),)
        self.user = None
        self.port = None
        self.script_name = 'create_ca.sh'
        self.zip_name = 'db-cert-replacement.zip'
        self.remote_path = None
        self.install_success_msg = 'start datanode success'

    def test_upgrade(self):
        text = '----prestep1:获取可用用户和端口----'
        self.log.info(text)
        self.user, self.port = self.com_install.get_user_and_port(
            *self.root_nodes)
        self.log.info(f'self.user:{self.user}, self.port:{self.port}')
        self.user_nodes[0].ssh_user = self.user
        self.user_nodes[0].ssh_password = macro.COMMON_PASSWD

        self.remote_path = os.path.join('/home', self.user)
        self.data_path = os.path.join('/home', self.user, 'data')
        self.app_path = os.path.join('/home', self.user, 'app')
        self.com_install.data_path = self.data_path
        self.com_install.app_path = self.app_path

        text = '----prestep2:创建数据库用户和用户组;expect:创建成功----'
        self.log.info(text)
        self.com_install.prepare_user(self.user, *self.root_nodes)

        text = '----step1:获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        res = self.com_install.tar_pkg(self.root_nodes[0])
        self.assertIn('install.sh', res, f'执行失败: {text}')

        text = '----step2:安装时ssl证书路径正确;expect:执行成功----'
        self.log.info(text)
        # 因生成证书过程中需要用到gs_guc encrypt命令，因此先安装然后卸载，重装时指定ssl证书路径
        text = '----step2.1: 安装; expect: 成功----'
        self.log.info(text)
        res = self.com_install.install_single(self.user_nodes[0], self.port)
        self.assertIn(self.install_success_msg, res, f'执行失败: {text}')

        text = '----step2.2: 将create_ca脚本发送到数据库主机; expect: 成功----'
        self.log.info(text)
        self.Common.scp_file(self.user_nodes[0], self.script_name,
                             self.remote_path)
        cmd = f'ls {self.remote_path}|grep {self.script_name}'
        self.log.info(cmd)
        res = self.user_nodes[0].sh(cmd).result()
        self.log.info(res)
        self.assertIn(self.script_name, res, f'执行失败: {text}')

        text = '----step2.3: 通过create_ca脚本生成证书; expect: 成功----'
        self.log.info(text)
        sh_cmd = f'cd {self.remote_path};' \
                 f'sh {self.script_name} {self.user} ' \
                 f'{macro.COMMON_PASSWD} ' \
                 f'{self.user_nodes[0].db_user};'
        self.log.info(sh_cmd)
        res = self.user_nodes[0].sh(sh_cmd).result()
        self.log.info(res)

        text = '----step2.4: 卸载; expect: 卸载成功----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(*self.user_nodes, mode='')
        self.assertIn('success', res, f'执行失败: {text}')

        text = '----step2.5: 查看证书文件; expect: 执行成功----'
        self.log.info(text)
        cmd = f'ls -l {self.remote_path};'
        self.log.info(cmd)
        res = self.root_nodes[0].sh(cmd).result()
        self.log.info(res)
        self.assertTrue(res.strip(), f'执行失败: {text}')

        text = '----step2.6: 安装时指定ssl证书路径; expect: 安装成功----'
        self.log.info(text)
        param = f'-D {self.data_path} -R {self.app_path} ' \
                f'-C "port={self.port}" --cert-path {self.remote_path}'
        res = self.com_install.install_single(self.user_nodes[0], self.port,
                                              param)
        self.assertIn(self.install_success_msg, res, f'执行失败: {text}')

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        res1 = self.com_uninstall.uninstall(*self.user_nodes)
        self.com_uninstall.clear_env(*self.root_nodes)
        self.assertIn('success', res1, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
